#define FOR_TEST  // 用于区分测试情况下的数据库路径(DBhandler.hpp)以及日志落地路径(log.hpp)

#include "../../include/sqlhelper.hpp"
#include "../../include/DBhandler.hpp"
#include <iostream>

// ----- photoDBHandler测试 -----
void insertDataIntoPhotosTable()
{
    // 打开数据库
    xzt::SqlHelper sql_helper(DB_PATHNAME);
    bool ret = sql_helper.open();
    if(!ret)
    {
        return;
    }
    // 获取数据位置
    std::string filename = "100.jpg";
    std::string file_path = "../../source/photos/" + filename;

    // 插入数据
    std::vector<std::string> columns = {"user_id", "title", "category", "image_path"};
    std::vector<std::string> values = {"1", filename, "red-photo", file_path};
    sql_helper.insert_data("photos", columns, values);

    // 关闭数据库
    sql_helper.close();
}

void _test_GetPhoto()
{
    xzt::PhotoDBHandler db_handler;
    std::string photo_pathname = "../../source/photos/100.jpg";
    auto photo = db_handler.GetPhoto(photo_pathname);
    if (photo.image.empty() || photo.image_path.empty())
    {
        LOG_ERROR("_test_GetPhoto failed");
    }
    else
    {
        LOG_INFO("_test_GetPhoto success");
        LOG_INFO("user_id: " + photo.user_id);
        LOG_INFO("title: " + photo.title);
        LOG_INFO("category: " + photo.category);
    }
}

void _test_GetPhotoByField()
{
    xzt::PhotoDBHandler db_handler;
    std::string user_id = "1";
    auto photos = db_handler.GetPhotosByUserID(user_id);
    if (photos.empty())
    {
        LOG_ERROR("_test_GetPhotoByField failed, by user_id");
    }
    else
    {
        for(auto photo: photos)
        {
            LOG_INFO("图像尺寸：%d x %d", photo.image.cols, photo.image.rows);
        }
        LOG_INFO("_test_GetPhotoByField success, by user_id");
    }

    std::string category = "red-photo";
    photos = db_handler.GetPhotosByCategory_OrderByTime(category);
    if (photos.empty())
    {
        LOG_ERROR("_test_GetPhotoByField failed, by category order by time");
    }
    else
    {
        for(auto photo: photos)
        {
            LOG_INFO("图像尺寸：%d x %d", photo.image.cols, photo.image.rows);
        }
        LOG_INFO("_test_GetPhotoByField success, by category order by time");
    }

    photos = db_handler.GetPhotosByCategory_OrderByLikes(category);
    if (photos.empty())
    {
        LOG_ERROR("_test_GetPhotoByField failed, by category order by likes");
    }
    else
    {
        for(auto photo: photos)
        {
            LOG_INFO("图像尺寸：%d x %d", photo.image.cols, photo.image.rows);
        }
        LOG_INFO("_test_GetPhotoByField success, by category order by likes");
    }
}

// 测试PhotoHandler
void test_PhotoHandler()
{
    _test_GetPhoto();
    _test_GetPhotoByField();
}

// ----- 测试userDBHandler -----
void insertDataIntoUsersTable()
{
    xzt::UserDataType user("zoufeng", "123456", "68696688@qq.com");
    xzt::UserDBHandler db_handler;
    int ret = db_handler.RegisterUser(user);
    if (ret == 1)
    {
        LOG_INFO("注册成功");
    }
    else if (ret == 0)
    {
        LOG_INFO("用户已存在");
    }
    else
    {
        LOG_ERROR("注册失败");
    }
}

void _test_LoginUser()
{
    std::string username = "zoufeng";
    std::string password = "123456";
    xzt::UserDBHandler db_handler;
    bool ret = db_handler.Verify(username, password);
    if(ret)
    {
        LOG_INFO("登录成功");
    }
    else
    {
        LOG_INFO("登录失败");
    }
}

void _test_GetUserByUsername()
{
    std::string username = "zoufeng";
    xzt::UserDBHandler db_handler;
    auto user = db_handler.GetUserByName(username);
    if(user.username.empty())
    {
        LOG_INFO("用户不存在");
    }
    else
    {
        LOG_INFO("用户名:%s,密码:%s,邮箱:%s", user.username.c_str(), user.password.c_str(), user.email.c_str());
    }
}

void _test_UpdateUser()  // 测试更新用户密码
{
    std::string username = "zoufeng";
    std::string password = "123456";
    std::string new_password = "654321";

    xzt::UserDBHandler db_handler;
    auto old_user = db_handler.GetUserByName(username);
    if(old_user.username.empty())
    {
        LOG_INFO("用户不存在");
        return;
    }
    
    old_user.password = new_password;
    bool ret = db_handler.UpdateUser(username, old_user);
    if(ret)
    {
        LOG_INFO("更新成功，新密码为:%s", new_password.c_str());
    }
    else
    {
        LOG_INFO("更新失败");
    }
}

void test_UserHandler()
{
    _test_LoginUser();
    _test_GetUserByUsername();
    _test_UpdateUser();
}

int main()
{
    // insertDataIntoPhotosTable();  // 首次调用用于插入测试数据
    // test_PhotoHandler();  // 测试PhotoHandler

    // insertDataIntoUsersTable();  // 首次调用用于插入测试数据
    test_UserHandler();  // 测试UserHandler

    return 0;
}
